define(['cookie', 'pagination', 'lazyload'], function(cookie) {
    return {
        init: function() {
            let $this = this;
            let array_default = [];
            let array = [];
            let prev1 = [];
            let next1 = [];
            $.ajax({
                type: "get",
                url: `http://localhost/wmsj/php/index.php`,
                dataType: "json"
            }).done((data) => {
                //console.log(1111);
                let $main = $('.main');
                let $ul = $('.leyi')
                    // console.log($main);
                let $indexhtml = `
                
                `;
                let $leyi = ``;
                let num = 1;
                let leyinum = 1;
                $.each(data, function(index, value) {
                    // console.log(1);

                    //  console.log(value);
                    if (value.classis == 'shangpin') {
                        if (num == 2 || num == 6) {

                            $indexhtml += `<div class="product-item list in" style="margin-right:0">
                            <a href="http://localhost/wmsj/src/html/details.html?sid=${index+1}"target="_blank">
                           
                            <img src="${value.url}" alt="" title="DOTA2 - 扭蛋手办 II"></a>
                            <p class="name ellipsis" title="${value.title}" style="overflow-wrap: break-word; word-break: break-all;">${value.title}</p>
                            <p class="price">¥${value.price}</p>
                            </div> `;

                            num++;
                        } else {


                            $indexhtml += `<div class="product-item list in">
                            <a href="http://localhost/wmsj/src/html/details.html?sid=${index+1}"target="_blank">
                            <img class="lazy" data-original="${value.url}"/></a>
                     <p class="name ellipsis" title="${value.title}" style="overflow-wrap: break-word; word-break: break-all;">${value.title}</p>
                     <p class="price">¥${value.price}</p>
                     </div> `;
                            num++;
                        }


                        //     console.log(num);
                    }


                    if (value.classis == 'leyi') {

                        //  console.log(value.title);
                        // console.log(value.title.split(','));
                        let arr = value.title.split(',');
                        // console.log(arr[0].split(':')[1]);
                        if (leyinum == 3 || leyinum == 6) {
                            $leyi += `
                                            <li class="in" style="margin-right: 0">
                        <a href="<a href="http://localhost/wmsj/src/html/details.html?sid=${index+1}"  target="_blank">
                            <img src="${value.url}" title="${arr}" class="f_left">
                            <div class="f_left info">
                                <div class="text ellipsis" title="${arr[0].split(':')[1]}" style="overflow-wrap: break-word; word-break: break-all;">${arr[1].split(':')[1]}</div>
                                <p class="from"><span></span>来自于<font>${arr[2].split(':')[1]}</font></p>
                                <p class="name ellipsis is-truncated" title="粘土人 痛苦女王  TI7周边预售  限时特价" style="overflow-wrap: break-word; word-break: break-all;">${arr[0].split(':')[1]} <span class="suffix" style="display: inline;">¥${value.price}</span></p>
                            </div>
                        </a>
                    </li>
`;
                            leyinum++;


                        } else {

                            leyinum++;
                            $leyi += `
                                            <li class="in" >
                                            <a href="<a href="http://localhost/wmsj/src/html/details.html?sid=${index+1}"    target="_blank">
                                            <img src="${value.url}" title="${arr}" class="f_left">
                                            <div class="f_left info">
                                                <div class="text ellipsis" title="${arr[0].split(':')[1]}" style="overflow-wrap: break-word; word-break: break-all;">${arr[1].split(':')[1]}</div>
                                                <p class="from"><span></span>来自于<font>${arr[2].split(':')[1]}</font></p>
                                                <p class="name ellipsis is-truncated" title="粘土人 痛苦女王  TI7周边预售  限时特价" style="overflow-wrap: break-word; word-break: break-all;">${arr[0].split(':')[1]} <span class="suffix" style="display: inline;">¥${value.price}</span></p>
                                            </div>
                                        </a>
                                    </li>
`;
                        }



                    }
                });




                $ul.append($leyi);
                $main.append($indexhtml);



                $(function() {

                    $("img.lazy").lazyload({ effect: "fadeIn" });
                    // $("img.lazy").lazyload({ threshold: -180 }); 





                });




<<<<<<< HEAD
            });
            console.log(1);
            if (cookie.get('users')) {
                $(function() {
                    // console.log(1);
                    let usersname = cookie.get('users');
                    // console.log(usersname);
                    $('.checkusers').eq(0).css({ 'display': 'none' });
                    $('.checkusers').eq(1).css({ 'display': 'block' });
                    $('.checkusers').eq(1).find('.names').text(usersname);
                    console.log($('.checkusers').eq(1).find('.names').text(usersname));
                });
            }



            /* *****插件****** */
            //console.log($('.page'));
            $('.page').pagination({
                pageCount: 3,
                jump: true,
                coping: true,
                prevContent: '上一页',
                nextContent: '下一页',
                homePage: '首页',
                endPage: '尾页',
                callback: function(api) {
                    // console.log(api.getCurrent());
                    $.ajax({
                        url: 'http://localhost/wmsj/php/indexpage.php',
                        data: {
                            page: api.getCurrent()
                        },
                        dataType: 'json'
                    }).done(function(data) {
                        //  console.log(data);
                        let $strhtml12 = '<ul>';
                        $.each(data, function(index, value) {
                            $strhtml12 += `
                                <li>
                                    <a href="detail.html?sid=${value.sid}" target="_blank">
                                    <img class="lazy" data-original="${value.url}" width="200" height="200"/>
                                        <p>${value.sid}${value.title}</p>
                                        <span class="price">￥${value.price}</span>
                                        <span>${value.sailnumber}</span>
                                    </a>
                                </li>
                            `;
                        });
                        $strhtml12 += '</ul>';
                        $('.listlazy').html($strhtml12); //当使用该方法设置一个值时，它会覆盖所有匹配元素的内容。
                        // $('.listlazy ul').append($strhtml12);
                        //2.添加懒加载
                        $(function() {
                            $("img.lazy").lazyload({ effect: "fadeIn" });
                            // $("img.lazy").lazyload({ threshold: -180 });
                            //$('.listlazy ul').append($strhtml12);
                        });



                        array_default = []; //排序前的li数组
                        array = []; //排序中的数组



                        $('.listlazy li').each(function(index, element) {
                            array[index] = $(this);
                            array_default[index] = $(this);

                        });
                        //console.log(array);




                        /* 默认排序 */
                        $('.fye1').on('click', function() {
                            $.each(array_default, function(index, value) {
                                $('.listlazy ul').append(value);
                            });
                        });

                        // console.log(array);
                        /* 升序   快速冒泡排序*/
                        $('.fye2').on('click', function() {

                            let $length = array.length;
                            let left = 0;
                            let right = $length - 1;
                            let Index = left;



                            prev1 = [];
                            next1 = [];
                            $.each(array, function(index, value) {

                                let arrval = parseFloat(array[index].find('.price').html().substring(1));
                                prev1[index] = arrval;
                                next1[index] = arrval;
                                // console.log(arrval); push hui ctu
                            });

                            //console.log(prev1);
                            //   console.log(prev1);

                            /*   function quicksort(arr, left, right) {
                               
                                if (left < right) {
                                    return arr;
                                }


                                for (let i = left + 1; i < right; i++) {
                                    if (+arr[left] > +arr[i]) {
                                        Index++;
                                        bumaopao(arr, Index, i);

                                    }

                                }
                                bumaopao(arr, Index, left);

                                quicksort(arr, left, Index + 1);
                                quicksort(arr, Index + 1, right);



                            };

                            function bumaopao(arr, i, j) {

                                var temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;

                            }
 */





                            function quickSort(arr, left, right) {
                                var len = arr.length,
                                    partitionIndex,
                                    left = typeof left !== 'number' ? 0 : left,
                                    right = typeof right !== 'number' ? len - 1 : right;

                                if (left < right) {

                                    partitionIndex = partition(arr, left, right);

                                    quickSort(arr, left, partitionIndex - 1);

                                    quickSort(arr, partitionIndex + 1, right);
                                }

                                return arr;
                            };

                            function partition(arr, left, right) {

                                var pivot = left,
                                    index = pivot + 1;
                                for (var i = index; i <= right; i++) {


                                    if (arr[i] < arr[pivot]) {
                                        swap(arr, i, index);
                                        index++;
                                    }
                                }

                                swap(arr, pivot, index - 1);

                                return index - 1;
                            };

                            function swap(arr, i, j) {
                                var temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                            };


                            let newarr = quickSort(next1, left, $length);
                            // console.log(newarr);
                            // $('.listlazy ul').append(newarr);

                            let lastarr = [];
                            // console.log(prev1);
                            //  console.log(newarr);
                            for (j = 0; j < array.length; j++) {
                                for (i = 0; i < array.length; i++) {
                                    if (prev1[j] == newarr[i]) {
                                        lastarr[i] = array[j];

                                    }

                                }

                            };
                            console.log(lastarr);


                            $.each(lastarr, function(index, value) {
                                //  console.log(value); //n.fn.init [li, context: li]
                                $('.listlazy ul').append(value);
                            });



                        });



                    })


                }




=======
>>>>>>> last commit
            });
            // console.log(1);
            if (cookie.get('users')) {
                $(function() {
                    // console.log(1);
                    let usersname1111 = cookie.get('users');
                    //   console.log(usersname1111);
                    // console.log(usersname);
                    $('.checkusers').eq(0).css({ 'display': 'none' });
                    $('.checkusers').eq(1).css({ 'display': 'block' });
                    $('.checkusers').eq(1).find('.names').text(usersname1111);
                    // console.log($('.checkusers').eq(1).find('.names').text(usersname));

                    /* *************************** */
                    /* ****************************************************************** */

                    let usersname = cookie.get('users');
                    //let datas = cookie.get('cookies');
                    //  console.log(usersname);
                    $.ajax({
                        type: 'post',
                        url: 'http://localhost/wmsj/php/regreturn.php',
                        dataType: 'json',
                        data: {
                            username: usersname,
                        }

                    }).done(function(data) {
                        console.log(data);
                        let data1 = '';

                        for (let key in data) {
                            if (key == 'cartlists') {
                                data1 = data[key];

                            }

                        }
                        console.log(data1);
                        let data2 = data1.split('-')[0];
                        console.log(data2);
                        cookie.set('cookies', data2, 24);

                        // console.log(1);
                        // console.log(String);
                        // let datas = JSON.stringify(data).split(',');
                        //console.log(datas);
                        // let datas1 = datas.split(',');
                        //console.log(datas1);
                        // let datasfin = '';


                        // for (let i = 0; i < datas1.length; i++) {
                        //     let key = datas1[i].split(':').join('"');

                        //     console.log(key);
                        //     // console.log(/^cartlists/.test(key));
                        //     // console.log(2);key == "cartlists"
                        //     if (key == "cartlists") {
                        //         console.log(1);
                        //         datasfin = datas1[i].split(':')[1];
                        //     }
                        // }
                        // console.log(datasfin);
                        // cookie.set('cookies', datasfin, 24);
                    })


                });

            }



            /* *****插件****** */
            //console.log($('.page'));
            $('.page').pagination({
                pageCount: 3,
                jump: true,
                coping: true,
                prevContent: '上一页',
                nextContent: '下一页',
                homePage: '首页',
                endPage: '尾页',
                callback: function(api) {
                    // console.log(api.getCurrent());
                    $.ajax({
                        url: 'http://localhost/wmsj/php/indexpage.php',
                        data: {
                            page: api.getCurrent()
                        },
                        dataType: 'json'
                    }).done(function(data) {
                        //  console.log(data);
                        let $strhtml12 = '<ul>';
                        $.each(data, function(index, value) {
                            $strhtml12 += `
                                <li>
                                    <a href="detail.html?sid=${value.sid}" target="_blank">
                                    <img class="lazy" data-original="${value.url}" width="200" height="200"/>
                                        <p>${value.sid}${value.title}</p>
                                        <span class="price">￥${value.price}</span>
                                        <span>${value.sailnumber}</span>
                                    </a>
                                </li>
                            `;
                        });
                        $strhtml12 += '</ul>';
                        $('.listlazy').html($strhtml12); //当使用该方法设置一个值时，它会覆盖所有匹配元素的内容。
                        // $('.listlazy ul').append($strhtml12);
                        //2.添加懒加载
                        $(function() {
                            $("img.lazy").lazyload({ effect: "fadeIn" });
                            // $("img.lazy").lazyload({ threshold: -180 });
                            //$('.listlazy ul').append($strhtml12);
                        });



                        array_default = []; //排序前的li数组
                        array = []; //排序中的数组



                        $('.listlazy li').each(function(index, element) {
                            array[index] = $(this);
                            array_default[index] = $(this);

                        });
                        //console.log(array);




                        /* 默认排序 */
                        $('.fye1').on('click', function() {
                            $.each(array_default, function(index, value) {
                                $('.listlazy ul').append(value);
                            });
                        });

                        // console.log(array);
                        /* 升序   快速冒泡排序*/
                        $('.fye2').on('click', function() {

                            let $length = array.length;
                            let left = 0;
                            let right = $length - 1;
                            let Index = left;



                            prev1 = [];
                            next1 = [];
                            $.each(array, function(index, value) {

                                let arrval = parseFloat(array[index].find('.price').html().substring(1));
                                prev1[index] = arrval;
                                next1[index] = arrval;
                                // console.log(arrval); push hui ctu
                            });

                            //console.log(prev1);
                            //   console.log(prev1);

                            /*   function quicksort(arr, left, right) {
                               
                                if (left < right) {
                                    return arr;
                                }


                                for (let i = left + 1; i < right; i++) {
                                    if (+arr[left] > +arr[i]) {
                                        Index++;
                                        bumaopao(arr, Index, i);

                                    }

                                }
                                bumaopao(arr, Index, left);

                                quicksort(arr, left, Index + 1);
                                quicksort(arr, Index + 1, right);



                            };

                            function bumaopao(arr, i, j) {

                                var temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;

                            }
 */





                            function quickSort(arr, left, right) {
                                var len = arr.length,
                                    partitionIndex,
                                    left = typeof left !== 'number' ? 0 : left,
                                    right = typeof right !== 'number' ? len - 1 : right;

                                if (left < right) {

                                    partitionIndex = partition(arr, left, right);

                                    quickSort(arr, left, partitionIndex - 1);

                                    quickSort(arr, partitionIndex + 1, right);
                                }

                                return arr;
                            };

                            function partition(arr, left, right) {

                                var pivot = left,
                                    index = pivot + 1;
                                for (var i = index; i <= right; i++) {


                                    if (arr[i] < arr[pivot]) {
                                        swap(arr, i, index);
                                        index++;
                                    }
                                }

                                swap(arr, pivot, index - 1);

                                return index - 1;
                            };

                            function swap(arr, i, j) {
                                var temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                            };


                            let newarr = quickSort(next1, left, $length);
                            // console.log(newarr);
                            // $('.listlazy ul').append(newarr);

                            let lastarr = [];
                            // console.log(prev1);
                            //  console.log(newarr);
                            for (j = 0; j < array.length; j++) {
                                for (i = 0; i < array.length; i++) {
                                    if (prev1[j] == newarr[i]) {
                                        lastarr[i] = array[j];

                                    }

                                }

                            };
                            console.log(lastarr);


                            $.each(lastarr, function(index, value) {
                                //  console.log(value); //n.fn.init [li, context: li]
                                $('.listlazy ul').append(value);
                            });



                        });



                    })


                }




            });













        },







<<<<<<< HEAD











        },







=======
>>>>>>> last commit
    }

})